package com.simulacra.model;

import java.util.HashMap;

import com.simulacra.applet.WinnerListener;// import com.simulacra.util.log.Logger;
/**
 * Clase que controla e inicia el generador de eventos
 * hasta completar la candidad de eventos que conforman un juego de Soccer.
 * 
 */
public class PlayoffSoccerGame extends SoccerGame implements EventListener {
	WinnerListener parent;
	public PlayoffSoccerGame(Team local, Team visitor) {
		setLocalTeam(local);
		setVisitorTeam(visitor);
		setEventGenerator(new SoccerEventGenerator(new HashMap<String, Double>() ,DEFAULT_EVENT_AMOUNT ) );


	}
	public PlayoffSoccerGame() {
		this(null, null);
	}
	public void setEventGenerator (EventGenerator eventGenerator) {
		super.setEventGenerator(eventGenerator);		
		getEventGenerator().addEventListener(this);// eventGenerator.addEventListener(this);

	}

	private void end (Team win) {
		if (win == null) {
			parent.log(this, "There is no winner");
		} else {
			parent.log (this, "*** the Winner is " + win.getName() + " ***");
		}
		parent.setWinner(win);
	}

	public void eventThrow(Object generator, Object event) {
		if (event != null && event instanceof SoccerEvent) {
			parent.log(this, "event " +((SoccerEvent)event).getEventName()+ " on favor of " + ((SoccerEvent)event).getSource().getName());
			if (SoccerEvent.GOL_EVENT_LABEL.equalsIgnoreCase(((SoccerEvent)event).getEventName())) {
				moveScore( ((SoccerEvent)event).getSource().getName(), 1);
			}
			if (SoccerEvent.END_EVENT_LABEL.equalsIgnoreCase(((SoccerEvent)event).getEventName())) {
				end( calculateWiningTeam());
			}
		}
		// TODO Auto-generated method stub
	}
	public Team calculateWiningTeam () {
		Team win= null;
		int localGol= 0;
		int visitorGol= 0;
		localGol = getLocalTeam()!= null? getLocalTeam().getScoredGoal():0;
		visitorGol = getVisitorTeam()!= null? getVisitorTeam().getScoredGoal():0;
		if (localGol > visitorGol) win= getLocalTeam();
		if (localGol < visitorGol) win= getVisitorTeam();
		return win== null?getLocalTeam():win;
	}
	private void moveScore (String team, int gol) {
		String teamName = "";
		if (Team.VISIT_TEAM.equalsIgnoreCase(team)) {
			getVisitorTeam().addGoal();
			teamName = getVisitorTeam().getName();
		}
		if (Team.LOCAL_TEAM.equalsIgnoreCase(team)) {
			getLocalTeam().addGoal();
			teamName = getLocalTeam().getName();
		}
		parent.log(this, "!!GOL!! " + teamName + " New Score: " + getVisitorTeam().getName() + " " +getVisitorTeam().getScoredGoal() + " - "+  getLocalTeam().getName() + " " +getLocalTeam().getScoredGoal());
	}
	public void init(WinnerListener reference) {
		parent= reference;
		parent.log(this, getLocalTeam().getName() + " V.S. " + getVisitorTeam().getName() );
		getEventGenerator().start();
	}

	public static void main (String [] argument) {	
		PlayoffSoccerGame game= null;
		EventGenerator generator;
		HashMap<String, Double>  map = null;
		Team local= null;
		Team visitor= null;
		local = new SoccerTeam("Monterrey",9);
		visitor = new SoccerTeam("Tigres",7);
		game = new PlayoffSoccerGame(local, visitor );
		map = new HashMap <String, Double> ();

		map.put(Team.VISIT_TEAM, new Double (0.2));
		map.put(Team.LOCAL_TEAM, new Double (0.8));
		map.put("Corner", new Double (0.1));
		map.put(SoccerEvent.GOL_EVENT_LABEL, new Double (0.05));
		map.put("Faul", new Double (0.3));
		map.put("Off Side", new Double (0.2));
		map.put("Amonestacion", new Double (0.1));
		map.put("Direct Shot", new Double (0.05));
		map.put("Band Throw", new Double (0.2));
		generator = new SoccerEventGenerator(map, DEFAULT_EVENT_AMOUNT);	
		game.setEventGenerator(generator);
		game.init(null);
	}
	public static void mani (String [] argument) {

		PlayoffSoccerGame game= null;
		EventGenerator generator;
		HashMap<String, Double>  map = null;

		Team local= null;
		Team visitor= null;
		local = new SoccerTeam("Monterrey",9);
		visitor = new SoccerTeam("Tigres",7);
		game = new PlayoffSoccerGame(local, visitor );

		map = new HashMap <String, Double> ();

		map.put(Team.VISIT_TEAM, new Double (0.2));
		map.put(Team.LOCAL_TEAM, new Double (0.8));
		map.put("Corner", new Double (0.1));
		map.put(SoccerEvent.GOL_EVENT_LABEL, new Double (0.05));
		map.put("Faul", new Double (0.3));
		map.put("Off Side", new Double (0.2));
		map.put("Amonestacion", new Double (0.1));
		map.put("Direct Shot", new Double (0.05));
		map.put("Band Throw", new Double (0.2));
		generator = new SoccerEventGenerator(map, DEFAULT_EVENT_AMOUNT);		
		generator.addEventListener(game);
		generator.start();
	}

}
